<?php

declare(strict_types=1);

namespace App\Chat\Service;

use App\Chat\Cache\FriendRemark;
use App\CustHub\Constants\ContactStatusConstants;
use App\CustHub\Model\ChContact;

class UserFriendService
{
    /**
     * 获取好友备注
     *
     * @param int $user_id   用户ID
     * @param int $friend_id 好友ID
     * @return string
     */
    public function getFriendRemark(int $user_id, int $friend_id): string
    {
        $remark = FriendRemark::getInstance()->read($user_id, $friend_id);
        if ($remark) return $remark;

        $remark = ChContact::query()->where('id', $friend_id)->value('remark');
        if ($remark) FriendRemark::getInstance()->save($user_id, $friend_id, $remark);

        return (string)$remark;
    }

    /**
     * 判断用户之间是否存在好友关系
     *
     * @param mixed $robotId
     * @param mixed $friend_id 好友ID
     * @return bool
     */
    public function isFriend(mixed $robotId, mixed $friend_id): bool
    {
        return ChContact::query()
            ->where('id', $friend_id)
            ->where('robot_id', $robotId)
            ->where('status', ContactStatusConstants::NORMAL_FRIEND)
            ->exists();
    }
}